home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlsub.1 < prev    next >
Text File  |  1995-07-25  |  12KB  |  331 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlsub - Perl subroutines
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           To declare subroutines:
  13.  
  14.               sub NAME;      # A "forward" declaration.
  15.               sub NAME BLOCK # A declaration and a definition.
  16.  
  17.           To import subroutines:
  18.  
  19.               use PACKAGE qw(NAME1 NAME2 NAME3);
  20.  
  21.           To call subroutines:
  22.  
  23.               &NAME          # Passes current @_ to subroutine.
  24.               &NAME(LIST);   # Parens required with & form.
  25.               NAME(LIST);    # & is optional with parens.
  26.               NAME LIST;     # Parens optional if predeclared/imported.
  27.  
  28.  
  29.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  30.           Any arguments passed to the routine come in as array @_,
  31.           that is ($_[0], $_[1], ...).  The array @_ is a local array,
  32.           but its values are references to the actual scalar
  33.           parameters.  The return value of the subroutine is the value
  34.           of the last expression evaluated, and can be either an array
  35.           value or a scalar value.  Alternately, a return statement
  36.           may be used to specify the returned value and exit the
  37.           subroutine.  To create local variables see the _l_o_c_a_l() and
  38.           _m_y() operators.
  39.  
  40.           A subroutine may called using the "&" prefix.  The "&" is
  41.           optional in Perl 5, and so are the parens if the subroutine
  42.           has been predeclared.  (Note, however, that the "&" is _N_O_T
  43.           optional when you're just naming the subroutine, such as
  44.           when it's used as an argument to _d_e_f_i_n_e_d() or _u_n_d_e_f().  Nor
  45.           is it optional when you want to do an indirect subroutine
  46.           call with a subroutine name or reference using the
  47.           &$subref() or &{$subref}() constructs.  See the _p_e_r_l_r_e_f
  48.           manpage for more on that.)
  49.  
  50.           Example:
  51.  
  52.               sub MAX {
  53.                   my $max = pop(@_);
  54.                   foreach $foo (@_) {
  55.                       $max = $foo if $max < $foo;
  56.                   }
  57.                   $max;
  58.               }
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))
  71.  
  72.  
  73.  
  74.               ...
  75.               $bestday = &MAX($mon,$tue,$wed,$thu,$fri);
  76.  
  77.           Example:
  78.  
  79.               # get a line, combining continuation lines
  80.               #  that start with whitespace
  81.  
  82.               sub get_line {
  83.                   $thisline = $lookahead;
  84.                   LINE: while ($lookahead = <STDIN>) {
  85.                       if ($lookahead =~ /^[ \t]/) {
  86.                           $thisline .= $lookahead;
  87.                       }
  88.                       else {
  89.                           last LINE;
  90.                       }
  91.                   }
  92.                   $thisline;
  93.               }
  94.  
  95.               $lookahead = <STDIN>;       # get first line
  96.               while ($_ = get_line()) {
  97.                   ...
  98.               }
  99.  
  100.           Use array assignment to a local list to name your formal
  101.           arguments:
  102.  
  103.               sub maybeset {
  104.                   my($key, $value) = @_;
  105.                   $foo{$key} = $value unless $foo{$key};
  106.               }
  107.  
  108.           This also has the effect of turning call-by-reference into
  109.           call-by-value, since the assignment copies the values.
  110.  
  111.           Subroutines may be called recursively.  If a subroutine is
  112.           called using the "&" form, the argument list is optional.
  113.           If omitted, no @_ array is set up for the subroutine; the @_
  114.           array at the time of the call is visible to subroutine
  115.           instead.
  116.  
  117.               &foo(1,2,3);        # pass three arguments
  118.               foo(1,2,3);         # the same
  119.  
  120.               foo();              # pass a null list
  121.               &foo();             # the same
  122.               &foo;               # pass no arguments--more efficient
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))
  137.  
  138.  
  139.  
  140.           PPPPaaaassssssssiiiinnnngggg SSSSyyyymmmmbbbboooollll TTTTaaaabbbblllleeee EEEEnnnnttttrrrriiiieeeessss
  141.  
  142.           [Note:  The mechanism described in this section works fine
  143.           in Perl 5, but the new reference mechanism is generally
  144.           easier to work with.  See the _p_e_r_l_r_e_f manpage.]
  145.  
  146.           Sometimes you don't want to pass the value of an array to a
  147.           subroutine but rather the name of it, so that the subroutine
  148.           can modify the global copy of it rather than working with a
  149.           local copy.  In perl you can refer to all the objects of a
  150.           particular name by prefixing the name with a star: *foo.
  151.           This is often known as a "type glob", since the star on the
  152.           front can be thought of as a wildcard match for all the
  153.           funny prefix characters on variables and subroutines and
  154.           such.
  155.  
  156.           When evaluated, the type glob produces a scalar value that
  157.           represents all the objects of that name, including any
  158.           filehandle, format or subroutine.  When assigned to, it
  159.           causes the name mentioned to refer to whatever "*" value was
  160.           assigned to it.  Example:
  161.  
  162.               sub doubleary {
  163.                   local(*someary) = @_;
  164.                   foreach $elem (@someary) {
  165.                       $elem *= 2;
  166.                   }
  167.               }
  168.               doubleary(*foo);
  169.               doubleary(*bar);
  170.  
  171.           Note that scalars are already passed by reference, so you
  172.           can modify scalar arguments without using this mechanism by
  173.           referring explicitly to $_[0] etc.  You can modify all the
  174.           elements of an array by passing all the elements as scalars,
  175.           but you have to use the * mechanism (or the equivalent
  176.           reference mechanism) to push, pop or change the size of an
  177.           array.  It will certainly be faster to pass the typeglob (or
  178.           reference).
  179.  
  180.           Even if you don't want to modify an array, this mechanism is
  181.           useful for passing multiple arrays in a single LIST, since
  182.           normally the LIST mechanism will merge all the array values
  183.           so that you can't extract out the individual arrays.
  184.  
  185.           OOOOvvvveeeerrrrrrrriiiiddddiiiinnnngggg bbbbuuuuiiiillllttttiiiinnnn ffffuuuunnnnccccttttiiiioooonnnnssss
  186.  
  187.           Many builtin functions may be overridden, though this should
  188.           only be tried occasionally and for good reason.  Typically
  189.           this might be done by a package attempting to emulate
  190.           missing builtin functionality on a non-Unix system.
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))
  203.  
  204.  
  205.  
  206.           Overriding may only be done by importing the name from a
  207.           module--ordinary predeclaration isn't good enough.  However,
  208.           the subs pragma (compiler directive) lets you, in effect,
  209.           predeclare subs via the import syntax, and these names may
  210.           then override the builtin ones:
  211.  
  212.               use subs 'chdir', 'chroot', 'chmod', 'chown';
  213.               chdir $somewhere;
  214.               sub chdir { ... }
  215.  
  216.           Library modules should not in general export builtin names
  217.           like "open" or "chdir" as part of their default @EXPORT
  218.           list, since these may sneak into someone else's namespace
  219.           and change the semantics unexpectedly.  Instead, if the
  220.           module adds the name to the @EXPORT_OK list, then it's
  221.           possible for a user to import the name explicitly, but not
  222.           implicitly.  That is, they could say
  223.  
  224.               use Module 'open';
  225.  
  226.           and it would import the open override, but if they said
  227.  
  228.               use Module;
  229.  
  230.           they would get the default imports without the overrides.
  231.  
  232.           AAAAuuuuttttoooollllooooaaaaddddiiiinnnngggg
  233.  
  234.           If you call a subroutine that is undefined, you would
  235.           ordinarily get an immediate fatal error complaining that the
  236.           subroutine doesn't exist.  (Likewise for subroutines being
  237.           used as methods, when the method doesn't exist in any of the
  238.           base classes of the class package.) If, however, there is an
  239.           AUTOLOAD subroutine defined in the package or packages that
  240.           were searched for the original subroutine, then that
  241.           AUTOLOAD subroutine is called with the arguments that would
  242.           have been passed to the original subroutine.  The fully
  243.           qualified name of the original subroutine magically appears
  244.           in the $AUTOLOAD variable in the same package as the
  245.           AUTOLOAD routine.  The name is not passed as an ordinary
  246.           argument because, er, well, just because, that's why...
  247.  
  248.           Most AUTOLOAD routines will load in a definition for the
  249.           subroutine in question using eval, and then execute that
  250.           subroutine using a special form of "goto" that erases the
  251.           stack frame of the AUTOLOAD routine without a trace.  (See
  252.           the standard AutoLoader module, for example.) But an
  253.           AUTOLOAD routine can also just emulate the routine and never
  254.           define it.  A good example of this is the standard Shell
  255.           module, which can treat undefined subroutine calls as calls
  256.           to Unix programs.
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLSSSSUUUUBBBB((((1111))))
  269.  
  270.  
  271.  
  272.           There are mechanisms available for modules to help them
  273.           split themselves up into autoloadable files to be used with
  274.           the standard AutoLoader module.  See the document on
  275.           extensions.
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.